package com.zhangxu.microservice.system.web.controller.ae;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.system.biz.dto.AssignRoleDTO;
import com.zhangxu.microservice.system.biz.dto.EmployeeRoleDTO;
import com.zhangxu.microservice.system.biz.service.EmployeeRoleService;
import com.zhangxu.microservice.system.dao.condition.EmployeeRoleCondition;
import com.zhangxu.microservice.system.web.convert.EmployeeRoleWebConverter;
import com.zhangxu.microservice.system.web.request.AssignRoleReq;
import com.zhangxu.microservice.system.web.request.CreateEmployeeRoleReq;
import com.zhangxu.microservice.system.web.request.PageReq;
import com.zhangxu.microservice.system.web.response.EmployeeRoleResp;
import com.zhangxu.microservice.system.web.response.PageResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工角色关系控制器
 */
@Tag(name = "员工角色关系管理", description = "员工角色关系相关接口")
@RestController
@RequestMapping("/ae/employee-roles")
@RequiredArgsConstructor
public class EmployeeRoleController {

    private final EmployeeRoleService employeeRoleService;
    private final EmployeeRoleWebConverter employeeRoleWebConverter;

    @Operation(summary = "为员工分配角色", description = "为指定员工分配多个角色")
    @PostMapping("/assign")
    public CommonResult<Void> assignRoles(@RequestBody @Validated AssignRoleReq assignRoleReq) {
        AssignRoleDTO assignRoleDTO = employeeRoleWebConverter.toAssignRoleDTO(assignRoleReq);
        employeeRoleService.assignRoles(assignRoleDTO);
        return CommonResult.success();
    }

    @Operation(summary = "添加员工角色关系", description = "为员工添加单个角色")
    @PostMapping
    public CommonResult<Long> addEmployeeRole(@RequestBody @Validated CreateEmployeeRoleReq createEmployeeRoleReq) {
        Long id = employeeRoleService.addEmployeeRole(createEmployeeRoleReq.getEmployeeId(), createEmployeeRoleReq.getRoleId());
        return CommonResult.success(id);
    }

    @Operation(summary = "删除员工角色关系", description = "根据关系ID删除员工角色关系")
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteEmployeeRole(
            @Parameter(description = "关系ID", required = true, example = "1")
            @PathVariable Long id) {
        employeeRoleService.deleteEmployeeRole(id);
        return CommonResult.success();
    }

    @Operation(summary = "根据员工ID和角色ID删除关系", description = "根据员工ID和角色ID删除员工角色关系")
    @DeleteMapping("/by-ids")
    public CommonResult<Void> deleteEmployeeRoleByIds(
            @Parameter(description = "员工ID", required = true, example = "1")
            @RequestParam Long employeeId,
            @Parameter(description = "角色ID", required = true, example = "1")
            @RequestParam Long roleId) {
        employeeRoleService.deleteEmployeeRoleByIds(employeeId, roleId);
        return CommonResult.success();
    }

    @Operation(summary = "删除员工的所有角色", description = "根据员工ID删除该员工的所有角色关系")
    @DeleteMapping("/by-employee/{employeeId}")
    public CommonResult<Void> deleteByEmployeeId(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId) {
        employeeRoleService.deleteByEmployeeId(employeeId);
        return CommonResult.success();
    }

    @Operation(summary = "删除角色的所有员工", description = "根据角色ID删除该角色的所有员工关系")
    @DeleteMapping("/by-role/{roleId}")
    public CommonResult<Void> deleteByRoleId(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable Long roleId) {
        employeeRoleService.deleteByRoleId(roleId);
        return CommonResult.success();
    }

    @Operation(summary = "获取员工的角色列表", description = "根据员工ID获取该员工的所有角色关系")
    @GetMapping("/by-employee/{employeeId}")
    public CommonResult<List<EmployeeRoleResp>> getByEmployeeId(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId) {
        List<EmployeeRoleDTO> employeeRoleDTOList = employeeRoleService.getByEmployeeId(employeeId);
        List<EmployeeRoleResp> respList = employeeRoleDTOList.stream()
                .map(employeeRoleWebConverter::toEmployeeRoleResp)
                .collect(Collectors.toList());
        return CommonResult.success(respList);
    }

    @Operation(summary = "获取角色的员工列表", description = "根据角色ID获取该角色的所有员工关系")
    @GetMapping("/by-role/{roleId}")
    public CommonResult<List<EmployeeRoleResp>> getByRoleId(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable Long roleId) {
        List<EmployeeRoleDTO> employeeRoleDTOList = employeeRoleService.getByRoleId(roleId);
        List<EmployeeRoleResp> respList = employeeRoleDTOList.stream()
                .map(employeeRoleWebConverter::toEmployeeRoleResp)
                .collect(Collectors.toList());
        return CommonResult.success(respList);
    }

    @Operation(summary = "分页查询员工角色关系", description = "分页查询员工角色关系列表")
    @GetMapping
    public CommonResult<PageResponse<EmployeeRoleResp>> employeeRolePage(
            @Valid PageReq pageReq,
            @Parameter(description = "员工ID") @RequestParam(value = "employeeId", required = false) Long employeeId,
            @Parameter(description = "角色ID") @RequestParam(value = "roleId", required = false) Long roleId) {

        EmployeeRoleCondition condition = new EmployeeRoleCondition();
        condition.setEmployeeId(employeeId);
        condition.setRoleId(roleId);
        condition.setPageNum(pageReq.getPageNum());
        condition.setPageSize(pageReq.getPageSize());

        IPage<EmployeeRoleDTO> page = employeeRoleService.employeeRolePage(condition);
        return CommonResult.success(PageResponse.convert(page, employeeRoleWebConverter::toEmployeeRoleResp));
    }
}